﻿using System;
using System.Collections.Generic;
using System.Text;
using WeatherSystem.Application.MsgHandler;
using WeatherSystem.Data.Response;
using WeatherSystem.Application;
using WeatherSystem.Application.Model;

namespace WeatherSystem.Application.ContentAdapter.EventAdapter
{
    class QrCodeAdapter : EventAdapterBase
    {
        public override string EventName => "scancode_waitmsg";

        public override ResponseMessageBase Handle(Message message)
        {
            var user_model = new UserModel();
            var device_model = new DeviceeModel();
            var msg = message as QrCodeEventMessage;
            var sn_code = msg.Result;
            var device_name = "";
            if(!device_model.Exists(sn_code,ref device_name))
            {
                return new TextMsg(msg.FromUserName, msg.ToUserName, Data.Convert.ToTimeStamp(DateTime.Now), "您扫描的二维码不是设备标识码，绑定失败。");
            }
            if(user_model.Exists(msg.FromUserName))
            {
                user_model.Rebind(msg.FromUserName, sn_code);
                return new TextMsg(msg.FromUserName, msg.ToUserName, Data.Convert.ToTimeStamp(DateTime.Now), "您的账号重新绑定到了设备：" + device_name);
            }
            else
            {
                user_model.Bind(msg.FromUserName, sn_code);
                return new TextMsg(msg.FromUserName, msg.ToUserName, Data.Convert.ToTimeStamp(DateTime.Now), "您的账号成功绑定到了设备：" + device_name);
            }
        }
    }
}
